package ro.aiesec.jdbc_class;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

import junit.framework.Assert;

import org.junit.Test;

 
/**
 * @author AIESEC TEAM
 *
 */
public class JDBCExample {

	/*
	 * INSERT INTO  `enterprise_db`.`Utilizatori` (
`UserID` ,
`Nume` ,
`Parola` ,
`Email`
)
VALUES (
NULL ,  'nume_test01',  '',  ''
);
*/
	
	@Test
	public void testAdaugaUtilizator () 
			throws ClassNotFoundException, SQLException {
		
		final String sNumeUtilizator="Bogdan";
		final String sParolaUtilizator="test_pass";
		final String sEmailUtilizator="bogdan.perian@gmail.com";
	
		if(!verificaUtilizatorAutentificat(sNumeUtilizator))
		{
			Statement stmt = DBManager.getConnection().createStatement();
			String query = "INSERT INTO Utilizatori (Nume,Parola,Email) VALUES('"
					+ DBManager.toSQLString(sNumeUtilizator)
					+ "','"
					+ DBManager.toSQLString(sParolaUtilizator)
					+ "','"
					+ DBManager.toSQLString(sEmailUtilizator) + "')";
			stmt.executeUpdate(query);
		}
	}
	
	/**
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	@Test
	public void testAutentificare () throws ClassNotFoundException, SQLException {
		final String sNumeUtilizator="Bogdan";
		boolean bAutentified = verificaUtilizatorAutentificat(sNumeUtilizator);
		Assert.assertTrue("Utilizatorul " + sNumeUtilizator
				+ " nu s-a autentificat", bAutentified);
	}

	/**
	 * @param sNumeUtilizator
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	private boolean verificaUtilizatorAutentificat(final String sNumeUtilizator)
			throws ClassNotFoundException, SQLException {
		boolean bAutentified=false;
		

		// Print all warnings
		for( SQLWarning warn = DBManager.getConnection().getWarnings(); 
				warn != null; warn = warn.getNextWarning() )
		{
			System.out.println( "SQL Warning:" ) ;
			System.out.println( "State  : " + warn.getSQLState()  ) ;
			System.out.println( "Message: " + warn.getMessage()   ) ;
			System.out.println( "Error  : " + warn.getErrorCode() ) ;
		}

		// Get a statement from the connection
		java.sql.Statement stmt = DBManager.getConnection().createStatement() ;

		// Execute the query
		String sSQL = "SELECT * FROM Utilizatori WHERE Nume='" + sNumeUtilizator+"'";
		ResultSet rs = stmt.executeQuery( sSQL ) ;

		// Loop through the result set
		if ( rs.next() )
			bAutentified=true;
        
		// Close the result set, statement and the connection
		rs.close() ;
		stmt.close() ;
		//DBManager.getConnection().close() ;
		return bAutentified;
	}
	
	@Test
	public void testPosteazaTweet () {
		//TODO
	}
	
	@Test
	public void testListeazaTweeturi () {}
	
//	@Test
//	public void testConnectTo_JDBC () throws SQLException, ClassNotFoundException {
// 
//		System.out.println("-------- MySQL JDBC Connection Testing ------------");
// 
//		Connection connection = getConnection(); 
//		System.out.println(""+connection);
//	}
}